package jdbc;

import jdbc.utill.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class JdbcDemo07 {
    public static void main(String[] args) throws Exception {
        // 准备数据
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        // 链接数据库查询
        Connection connection= DButil.getConnection();
        String sql = "select username,nickname,password,age from t_user" +
                " where username=? and password=?";
        PreparedStatement ps= connection.prepareStatement(sql);
        // 替换占位符 参数
        ps.setString(1,name);
        ps.setString(2,password);
        ResultSet resuleSet =  ps.executeQuery();
        // 判断结果
        if(resuleSet.next()){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }
        DButil.close(connection);
    }
}
